#!/bin/bash -e
########### Description #############

# utility functions and variables shared with all baseline scripts
# to use the utility functions, the line
# . utils
# must be added in the beginning of the baseline script file

########### Variables #############

rootdir=$HOME/baseline-testing  # destination for coin source code and working directory
repodir=$rootdir/tqtc-coin-ci  # coin source code destination directory
vmbuilder_ip=10.212.3.36  # vmbuilder ip where the test server should be run
webserver_port=$QT_CI_WEBSERVER_PORT  # coin webserver port
webserver_port_default=8090  # the port where coin web front-end can be accessed

########### Functions #############

check_network_interface() {
# verify
# 1) webserver is run on the right server address
# 2) webserver port environment variable is set to avoid fallback to production port (8080)
# 3) webserver port is set to a fixed number
 if [[ "$(hostname -I)" != *"$vmbuilder_ip"* ]]; then
   echo "ERROR: Test server has to be run on the vmbuilder server!"
   exit 2;
 fi
 if [[ $webserver_port != $webserver_port_default ]]; then
  # to verify that other users can see the integration test progress, we make sure
  # to always use the same test server http address
  echo "ERROR: \$webserver_port must be set to" $webserver_port_default
  exit 2
 fi
}

ask_user_to_exec() {
 # prompts user before executing arguments enclosed within quotes
 # $1 ~ Message to be displayed in the beginning of prompt, eg. "Execute this function?"
 # $2: ~ Arguments to be executed, eg. "make -j1"
 # options:
 # Y,y - Continues executing the given arguments
 # N,n - Will set skip variable to 1 without executing the arguments
 # Q,q - Will terminate the bash session without executing the arguments
 args=${@:2} # executed arguments, only
 while true; do
    read -p "$1" yn
    case $yn in
        [Yy]* ) ${args[@]}; break;;
        [Nn]* ) skip=1; break;;
        [Qq]* ) exit 0; break;;
        * ) echo "Please answer (y)es, (n)o or (q)uit.";;
    esac
 done
}

is_user_vmbuilder() {
 # make sure that we do not accidentially try to run the script as vmbuilder
 if [[ "$USER" == "vmbuilder" ]]; then
  echo "ERROR: Do not try to execute webserver as vmbuilder user!"
  exit 2
 fi
}

function display_webserver_link() {
  if [ "$1" == "local" ]; then
   webserver_ip=localhost
  else
   webserver_ip=$vmbuilder_ip
  fi
  if [ -z $webserver_port ]; then
   webserver_port=8080
  fi
  echo "To see Coin status on your browser, open link:" http://$webserver_ip:$webserver_port/coin
}
